package com.my.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author : jhxue
 * @create_time : 2023/7/6 16:14
 * @Description
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private final String update_time = "updateTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        //创建时间
        String create_time = "createTime";
        if (metaObject.hasSetter(create_time)) {
            Object createTime = getFieldValByName(create_time, metaObject);
            if (createTime == null) {
                this.strictInsertFill(metaObject, create_time, LocalDateTime.class, LocalDateTime.now());
            }
        }
        //修改时间
        if (metaObject.hasSetter(update_time)) {
            Object updateTime = getFieldValByName(update_time, metaObject);
            if (updateTime == null) {
                this.strictInsertFill(metaObject, update_time, LocalDateTime.class, LocalDateTime.now());

            }
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //修改时间
        if (metaObject.hasSetter(update_time)) {
            Object updateTime = getFieldValByName(update_time, metaObject);
            if (updateTime == null) {
                this.strictUpdateFill(metaObject, update_time, LocalDateTime.class, LocalDateTime.now());
            }
        }
    }

}
